package com.cnc.service.Impl;

import com.cnc.dao.UserMapper;
import com.cnc.pojo.User;
import com.cnc.service.UserService;
import com.cnc.utils.JsonUtil;
import com.cnc.utils.PageUtil;
import com.cnc.utils.ResultUtil;
import com.cnc.utils.SqlUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class UserServiceImpl implements UserService {

    @Override
    public String Login(String username, String password, String code, HttpSession session) {
        ResultUtil resultUtil = null;
        SqlSession sqlSession = SqlUtil.getSqlSession();
        if(code.equalsIgnoreCase((String)session.getAttribute("verCode"))){
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            Object uid = mapper.Login(username, password);
            if(uid!=null){
                if(mapper.getStatus((int)uid)==1){
                    if(mapper.getRoles((int)uid)==1){
                        resultUtil=new ResultUtil(1,"登陆成功！",null);
                        session.setAttribute("user",username);
                    }else {
                        resultUtil=new ResultUtil(2,"您还不是管理员！",null);
                    }
                }else {
                    resultUtil=new ResultUtil(3,"您的账号被禁用,请联系管理员解封！",null);
                }
            }else {
                resultUtil=new ResultUtil(4,"账户或密码错误！",null);
            }
        }else {
            resultUtil=new ResultUtil(5,"验证码错误！",null);
        }
        sqlSession.close();
        return JsonUtil.objectToJson(resultUtil);
    }

    @Override
    public String getAdminList(String currentPage, String pageSize, String search) {
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        PageUtil pu = new PageUtil(pageSize, currentPage);
        List<User> list = mapper.getAdminList(pu.getOff(), pu.getLen(), search);
        int rows = mapper.getRows(search);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",rows);
        map.put("list",list);
        sqlSession.close();
        return JsonUtil.objectToJson(map);
    }

    @Override
    public String getUserInfo(String id) {
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(Integer.parseInt(id));
        sqlSession.close();
        return JsonUtil.objectToJson(user);
    }

    @Override
    public String updateUser(String data) {
        User user = (User)JsonUtil.JsonToObject(data, User.class);
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.updateUser(user);
        sqlSession.commit();
        sqlSession.close();
        if(i!=0){
            return "修改成功！";
        }else {
            return "修改失败！";
        }
    }

    @Override
    public String addUser(String data) {
        User user = (User)JsonUtil.JsonToObject(data, User.class);
        user.setCreatetime(new Date());
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.addUser(user);
        sqlSession.commit();
        sqlSession.close();
        if(i!=0){
            return "添加成功！";
        }else {
            return "添加失败！";
        }
    }

    @Override
    public String delAllSelect(String data) {
        ArrayList<Integer> list = (ArrayList<Integer>) JsonUtil.JsonToObject(data, Object.class);
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        list.forEach(i->mapper.delUserById(i));
        sqlSession.commit();
        return "删除成功！";
    }

    @Override
    public boolean register(String data) {
        User user = (User)JsonUtil.JsonToObject(data, User.class);
        user.setCreatetime(new Date());
        user.setStatus(1);
        user.setRole(2);
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.addUser(user);
        sqlSession.commit();
        sqlSession.close();
        if(i!=0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean checkPhone(String phone) {
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.checkPhone(phone);
        if(i!=0){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public String userLogin(String phone, String pwd) {
        ResultUtil resultUtil = null;
        SqlSession sqlSession = SqlUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.userLogin(phone, pwd);
        if(user!=null){
            if(user.getStatus()==1){
                resultUtil=new ResultUtil(1,"登陆成功！",user);
            }else {
                resultUtil=new ResultUtil(2,"您的账号被禁用,请联系管理员解封！",null);
            }
        }else {
            resultUtil=new ResultUtil(3,"密码错误!",null);
        }
        sqlSession.close();
        return JsonUtil.objectToJson(resultUtil);
    }
}
